﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SupportWebsite.Models;
using System.Net.Mail;
using System.Data.Objects;

namespace SupportWebsite.Controllers
{
    public class SmartTechController : Controller
    {
        //
        // GET: /SmartTech/

        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult OnlineRegistration(SmartTechParticipantModel participant)
        {
            SupportDBModel model = new SupportDBModel();
            Random Rand = new Random();
            if (ModelState.IsValid)
            {
                int? tmpId = model.CheckSmartTechParticipant(participant.Email, participant.Mobile).FirstOrDefault();
                if (tmpId != null)
                {
                    ViewBag.Workshops = model.GetSmartTechWorkshop();
                    ViewBag.Schools = model.GetColleges();
                    ViewBag.Interviews = model.GetSmartTechInterviews();
                    participant.Errors = "This is Email or Mobile already registered!!!!!!!";
                    return View("OnlineRegistration", participant);
                }
                model.AddSmartTechParticipant(participant.Name.Trim(), participant.Mobile.Trim(), participant.Email.Trim(), participant.School, participant.Workshop, participant.Interview, true);

                model.EditSmartTechInterviews(participant.Interview, -1);
                model.SaveChanges();
                
                int? Id = model.GetSmartTechParticipantIdByMobile(participant.Mobile).FirstOrDefault();
                string barcode = "A" + Id.ToString();
                barcode += "-" + Rand.Next().ToString();
                model.AddSmartTechParticipantBarcode(barcode, Id);
                model.SaveChanges();
                
                SendMail(barcode);
                return View("../Thanks");
            }
            else
            {
                ViewBag.Workshops = model.GetSmartTechWorkshop();
                ViewBag.Schools = model.GetColleges();
                ViewBag.Interviews = model.GetSmartTechInterviews();
                return View("OnlineRegistration", participant);
            }
        }

        public ActionResult Registration(SmartTechParticipantModel participant)
        {
            SupportDBModel model = new SupportDBModel();
            Random Rand = new Random();
            if (ModelState.IsValid)
            {
                int? tmpId = model.CheckSmartTechParticipant(participant.Email, participant.Mobile).FirstOrDefault();
                if (tmpId != null)
                {
                    ViewBag.Workshops = model.GetSmartTechWorkshop();
                    ViewBag.Schools = model.GetColleges();
                    ViewBag.Interviews = model.GetSmartTechInterviews();
                    participant.Errors = "This is Email or Mobile already registered!!!!!!!";
                    return View("Registration", participant);
                }
                model.AddSmartTechParticipant(participant.Name.Trim(), participant.Mobile.Trim(), participant.Email.Trim(), participant.School, participant.Workshop, participant.Interview, false);

                model.EditSmartTechInterviews(participant.Interview, -1);
                model.SaveChanges();

                int? Id = model.GetSmartTechParticipantIdByMobile(participant.Mobile).FirstOrDefault();
                string barcode = "B" + Id.ToString();
                barcode += "-" + Rand.Next().ToString();
                model.AddSmartTechParticipantBarcode(barcode, Id);
                model.SaveChanges();

                //SendMail(barcode);
                return Print(barcode);
            }
            else
            {
                ViewBag.Workshops = model.GetSmartTechWorkshop();
                ViewBag.Schools = model.GetColleges();
                ViewBag.Interviews = model.GetSmartTechInterviews();
                return View("Registration", participant);
            }
        }
        
        public ActionResult Print(string Mobile)
        {
            ViewBag.Name = "Mobile";
            if (Mobile == null)
            {
                ViewBag.Title = "Print";
                ViewBag.Error = "Enter The Mobile";
                return View("Edit");
            }
            else
            {
                SupportDBModel model = new SupportDBModel();
                string Barcode = model.GetSmartTechParticipantBarcodeByMobile(Mobile).FirstOrDefault().Trim();
                ViewBag.Participant = model.GetSmartTechParticipantByBarcode(Barcode).FirstOrDefault();
                if (ViewBag.Participant == null)
                {
                    ViewBag.Errors = "This Mobile is not registered!!!.";
                    return View("Edit");
                }
                return View("Print");
            }
            
        }

        public ActionResult Edit(string Mobile, SmartTechParticipantModel participant)
        {
            ViewBag.Name = "Mobile";
            SupportDBModel model = new SupportDBModel();
            if (Mobile == null)
            {
                ViewBag.Errors = "Enter The Barcode.";
                return View("Edit");
            }
            string Barcode = model.GetSmartTechParticipantBarcodeByMobile(Mobile).FirstOrDefault().Trim();
            Barcode = Barcode == null ? Request["Barcode"] : Barcode;
            if (Barcode == null || Barcode.Trim() == "")
            {
                if (model.CheckSmartTechParticipant("", Barcode) == null)
                {
                    ViewBag.Errors = "This Barcode is not registered!!!.";
                }
                else
                {
                    ViewBag.Errors = "Enter The Barcode.";
                }
                return View("Edit");
            }
            if (ModelState.IsValidField("Name") && ModelState.IsValidField("Email") && ModelState.IsValidField("Mobile"))
            {
                int? School = ((participant.School == 0) ? 
                    model.GetCollegeByName(Request["OldSchool"]).FirstOrDefault() : participant.School);
                int? Workshop = ((participant.Workshop == 0) ? 
                    model.GetSmartTechWorkshopByName(Request["OldWorkshop"]).FirstOrDefault() : participant.Workshop);
                int? Interview = ((participant.Interview == 0) ? 
                    model.GetSmartTechInterviewByTime(Request["OldInterview"]).FirstOrDefault() : participant.Interview);
                if(participant.Interview != 0)
                {
                    model.EditSmartTechInterviews(participant.Interview, -1);
                    model.EditSmartTechInterviews(model.GetSmartTechInterviewByTime(Request["OldInterview"]).FirstOrDefault(), 1);
                }
                model.EditSmartTechParticipantByBarcode(Barcode, participant.Name, participant.Mobile, participant.Email, School , Workshop, Interview);
                model.SaveChanges();
                ViewBag.Errors = "Editing Done";
                return View("Edit");
            }
            else
            {
                ViewBag.Participant = model.GetSmartTechParticipantByBarcode(Barcode).FirstOrDefault();
                if (ViewBag.Participant == null)
                {
                    ViewBag.Errors = "This Barcode is not registered!!!.";
                    return View("Edit");
                }
                ViewBag.Workshops = model.GetSmartTechWorkshop();
                ViewBag.Schools = model.GetColleges();
                ViewBag.Interviews = model.GetSmartTechInterviews();
                ViewBag.Barcode = Barcode;
                return View("EditParticipant");
            }
        }

        public ActionResult Delete(string Mobile, SmartTechParticipantModel participant)
        {
            ViewBag.Name = "Mobile";
            SupportDBModel model = new SupportDBModel();
            if (Mobile == null)
            {
                ViewBag.Errors = "Enter The Barcode.";
                return View("Edit");
            }
            string Barcode = model.GetSmartTechParticipantBarcodeByMobile(Mobile).FirstOrDefault().Trim();
            Barcode = Barcode == null ? Request["Barcode"] : Barcode;
            if (Barcode == null || Barcode.Trim() == "")
            {
                if (model.CheckSmartTechParticipant("", Barcode) == null)
                {
                    ViewBag.Errors = "This Barcode is not registered!!!.";
                }
                else
                {
                    ViewBag.Errors = "Enter The Barcode.";
                }
                return View("Edit");
            }
            else
            {
                model.EditSmartTechInterviews(model.GetSmartTechInterviewIdByBarcode(Barcode).FirstOrDefault(), 1);
                model.DeleteSmartTechParticipantbyBarcode(Barcode);
                model.SaveChanges();
                ViewBag.Errors = "Delete Done";
                return View("Edit");
            }
        }

        public ActionResult SendMail(string Barcode)
        {
            ViewBag.Name = "Barcode";
            SupportDBModel model = new SupportDBModel();
            if (Barcode == null)
            {
                ViewBag.Errors = "Enter The Barcode.";
                return View("Edit");
            }
            vSmartTechParticipant Participant = model.GetSmartTechParticipantByBarcode(Barcode).FirstOrDefault();
            if (Participant == null)
            {
                ViewBag.Errors = "This Barcode is not registered!!!.";
                return View("Edit");
            }
            else if (Barcode.Trim() == "")
            {
                ViewBag.Errors = "Enter The Barcode.";
                return View("Edit");
            }
            Barcode = Barcode.Trim();
            SmtpClient SmtpServer = new SmtpClient();
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("support.2013@live.com", "Support ASU");
            msg.To.Add(Participant.Email);
            msg.Subject = "Welcome to SmarTech";
            msg.Body = "Dear: " + Participant.Name + "<br/><br/>"
                        + "<a>    </a>Thank you for your registration for Smartech. We are so pleased<br/>"
                        + "with your request of joining Smartech event<br/><br/>"
                        + "Here is your barcode: <h3>" + Participant.Barcode + "</h3><br/>"
                        + "We will send you an e-mail soon to inform you with the<br/>"
                        + "interview’s location<br/><br/>"
                        + "Please go through this <a href=\"https:/" + "/www.support-asu.org/SmatTech/Print?Barcode=" + Participant.Barcode + ">link</a>, print<br/>"
                        + "the page which will apper and bring it with you.<br/><br/>"
                        + "If you have any further questions (queries) or comments, please<br/>"
                        + "feel free (don't hesitate) to contact us anytime on any of the<br/>"
                        + "following links:<br/>"
                        + "Our Website: https://www.support-asu.org <br/>"
                        + "Our Facebook page: https://www.facebook.com/SUPPORT.FCIS <br/>"
                        + "Our Twitter account: https://twitter.com/#!/SupportASU <br/><br/>"
                        + "Yours sincerely,<br/>"
                        + "IT-TEAM<br/>"
                        + "SUPPORT’13 “Smartech“<< the best you can get>>";
            SmtpServer.Send(msg);
            ViewBag.Errors = "The Message Sent.";
            return View("Edit");
        }

        public string GetInterviews()
        {
            string InterviewTimes = "";
            SupportDBModel model = new SupportDBModel();
            SmartTechInterview [] Interviews = model.GetSmartTechInterviews().ToArray();
            InterviewTimes += Interviews.Length.ToString();
            foreach (var item in Interviews)
            {
                InterviewTimes += "|";
                InterviewTimes += item.Id;
                InterviewTimes += "|";
                InterviewTimes += item.InterviewTime;
            }
            return InterviewTimes;
        }

        public ActionResult AddInterviews(string Day, string Date, string Hour, int? count)
        {
            if (Day == null || Date == null || Hour == null || count <= 0)
            {
                ViewBag.Errors = "Please Enter The Data";
                return View("AddInterviews");
            }
            else
            {
                string Interview = Day + " / " + Date + " / " + Hour;
                SupportDBModel model = new SupportDBModel();
                model.AddInterviewTime(Interview, count);
                ViewBag.Errors = "Done!!! more?";
                return View("AddInterviews");
            }
        }

        public ActionResult DeleteInterview(int? Interview)
        {
            if (Interview == null)
            {
                ViewBag.Errors = "Please Enter The Data";
                return View("DeleteInterviews");
            }
            else
            {
                SupportDBModel model = new SupportDBModel();
                model.DeleteInterviewTime(Interview);
                ViewBag.Errors = "Done!!! more?";
                return View("DeleteInterviews");
            }
        }
    }
}
